import Tishi from '../alert/index'
export default (Vue) => {
    Vue.directive('login', {
        bind(el, obj, vnode, preVnode) {
            el.addEventListener('click', () => {
                if (!localStorage.getItem('token')) obj.value.$router.push('/login')
            })
        }
    })
    Vue.directive('phone', {
        bind(el, obj, vnode, preVnode) {
            el.addEventListener('click', () => {
                var zhengze = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
                if (obj.value.form.phone.trim() == '') {
                    Tishi.TiShi({ text: '请输入手机号' })
                    return
                }
                if (!zhengze.test(obj.value.form.phone)) {
                    Tishi.TiShi({ text: '手机号码格式错误' })
                    return
                }
                obj.value.yes()
                let sec = 60
                el.style = `background-color: #F87F97;`
                el.disabled = true
                el.innerText = sec + 's'
                let clear = setInterval(() => {
                    sec--
                    el.innerText = sec + 's'
                    if (sec == 0) {
                        clearInterval(clear)
                        el.style = ``
                        el.innerText = '获取验证码'
                        el.disabled = false
                    }
                }, 1000)
            })
        }
    })
    Vue.directive('email', {
        bind(el, obj, vnode, preVnode) {
            el.addEventListener('click', () => {
                var reg = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
                if (reg.test(obj.value.form.email)) {
                    obj.value.submit_login()
                } else {
                    if (obj.value.form.email) {
                        Tishi.TiShi({ text: '邮箱格式不对' })
                    }
                }
            })
        }
    })
    Vue.directive('null', {
        bind(el, obj, vnode, preVnode) {
            el.addEventListener('click', () => {
                let flag = Object.keys(obj.value.form).every(item => {
                    return obj.value.form[item]
                })
                if (flag) {
                    obj.value.yes()
                    el.style = `background-color: #F87F97;`
                    el.disabled = true
                    setTimeout(() => {
                        el.style = ``
                        el.disabled = false
                    }, 3000)
                } else {
                    Tishi.TiShi({ text: '请输入必填字段' })
                }
            })
        }
    })
    Vue.directive('bac', {
        bind(el, obj, vnode, preVnode) {
            el.addEventListener('click', () => {
                el.style = 'background-color: #f8f9fa; transition: all 0.2s;'
                setTimeout(() => {
                    el.style = 'background-color: #fff; transition: all 0.2s;'
                }, 200)
            })
        }
    })
}